package com.ch.light.web.service.impl;

import com.ch.light.web.entity.RoleMenu;
import com.ch.light.web.entity.pojo.MenuTitle;
import com.ch.light.web.model.Menu;
import com.ch.light.web.model.Role;
import com.ch.light.web.service.MenuService;
import com.ch.light.web.service.RoleMenuService;
import com.ch.light.web.service.RoleService;
import com.ch.utils.CommonUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Override
    public List<RoleMenu> getList(String roleId) {
        checkArgument(!Strings.isNullOrEmpty(roleId), "角色编号不能为空");
        Map<String, Object> resultMap = Maps.newHashMap();
        List<MenuTitle> titleList = new ArrayList<>();
        List<Menu> titles = menuService.getByParentId("#");
        for (Menu menu : titles) {
            MenuTitle title = new MenuTitle();
            List<Menu> resources = menuService.getByParentId(menu.getCode());

            if (resources != null && resources.size() > 0) {
                resources.sort((e1, e2) -> CommonUtils.compareTo(e1.getSort(), e2.getSort()));
            }
            title.setMenuList(resources);
            title.setName(menu.getName());
            title.setSeq(menu.getSort());
            title.setTitleId(menu.getCode());
            titleList.add(title);
        }

        List<String> resIds = getMenuByRoleId(roleId);
        resultMap.put("titleList", titleList);
        resultMap.put("resIds", resIds);
        return null;
    }

    @Transactional
    @Override
    public void update(String roleId, String[] menuIds) {
        checkArgument(!Strings.isNullOrEmpty(roleId), "角色编号不能为空");
        Role role = roleService.getByRoleId(roleId);
        checkNotNull(role, "角色不存在");
        if (CommonUtils.isEmpty(menuIds)) {
            // 删除所有的角色-菜单数据
            roleService.removeAssignMenuByRoleId(roleId);
        } else {
            // 原来的角色-菜单,再批量插入
            List<String> addAssignMenuIdList;
            List<String> removeAssignMenuIdList = Lists.newArrayList();
            List<String> menuCodeList = menuService.findCodeByRoleId(roleId);
            if (CommonUtils.isEmpty(menuCodeList)) {
                addAssignMenuIdList = Lists.newArrayList(menuIds);
            } else {
                addAssignMenuIdList = Lists.newArrayList(menuIds);
                removeAssignMenuIdList = Lists.newArrayList(menuCodeList);
                // 过滤要删除分配权限
                removeAssignMenuIdList.removeAll(addAssignMenuIdList);
                // 过滤要添加分配权限
                addAssignMenuIdList.removeAll(menuCodeList);
            }

            if (CommonUtils.isNotEmpty(addAssignMenuIdList)) {
                roleService.assignMenus(roleId, addAssignMenuIdList);
            }
            if (CommonUtils.isNotEmpty(removeAssignMenuIdList)) {
                roleService.reassignMenus(roleId, removeAssignMenuIdList);
            }
        }
    }

    @Override
    public List<String> getMenuByRoleId(String roleId) {
        checkArgument(!Strings.isNullOrEmpty(roleId), "角色不能为空");
        return menuService.findCodeByRoleId(roleId);
    }

    @Override
    public List<RoleMenu> findByRoleId(String roleId) {
        return null;
    }
}
